package com.xc.h05.controller.wyl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Appointment;
import com.xc.h05.service.wyl.AppointmentService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/appointment")
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    @GetMapping("/findAll")
    public R findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Appointment> list = appointmentService.findAll();
        PageInfo<Appointment> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @PostMapping("/add")
    public R insert(@RequestBody Appointment appointment) {
        int result = appointmentService.insert(appointment);
        if (result > 0) {
            return R.ok("插入成功");
        } else {
            return R.fail("插入失败");
        }
    }

    @DeleteMapping("/delete")
    public R deleteByPrimaryKey(Long id) {
        System.out.println(id);
        int result = appointmentService.deleteByPrimaryKey(id);
        if (result > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }


    //wc修改
    @GetMapping("/deletewc")
    public R delete(Long id) {
        System.out.println(id);
        int result = appointmentService.deleteByPrimaryKey(id);
        if (result > 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @PostMapping("/select")
    public R insertSelective(@RequestBody Appointment appointment) {
        int result = appointmentService.insertSelective(appointment);
        if (result > 0) {
            return R.ok("有选择地插入成功");
        } else {
            return R.fail("有选择地插入失败");
        }
    }

    @GetMapping("/{id}")
    public R selectByPrimaryKey(@PathVariable Long id) {
        Appointment appointment = appointmentService.selectByPrimaryKey(id);
        if (appointment != null) {
            return R.ok(appointment);
        } else {
            return R.fail("未查询到对应课程购买记录");
        }
    }

    @PutMapping("/update")
    public R updateByPrimaryKeySelective(@RequestBody Appointment appointment) {
        int result = appointmentService.updateByPrimaryKeySelective(appointment);
        if (result > 0) {
            return R.ok("更新成功");
        } else {
            return R.fail("更新失败");
        }
    }
    // 根据多个status查询
    @PostMapping("/selectByStatus")
    public R findByStatus(@RequestBody Appointment appointment) {
        List<Appointment> list = appointmentService.findByIds(appointment.getStatusList(),appointment.getUserId());
            return R.ok(list);
    }

    // 新增接口
    @GetMapping("/consultInfo")
    public R findConsultInfo() {
        List<Map<String, Object>> consultInfo = appointmentService.findConsultInfo();
        return R.ok(consultInfo);
    }
}

